﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AzureProvider.ssdsClient;
using BlogEngine.Core;

namespace AzureProvider.SoupMethods
{
    public class PostCommentMethods : PostRelatedMethods
    {

        protected override string containerId
        {
            get { return "Posts"; }
        }

        protected override string kind
        {
            get { return "PostComment"; }
        }

        protected override Entity CreateEntity(object Object)
        {
            Comment myobj = (Comment)Object;

            var entity = new Entity()
            {
                Id = myobj.Id.ToString() ,
                Kind = kind
            };  
            //PostCommentID, CommentDate, Author, Email, Website, Comment, Country, Ip, IsApproved

            entity.Properties = new Dictionary<string, object>();
            
            entity.Properties.Add("Author", myobj.Author);
            entity.Properties.Add("Email", myobj.Email);
            
            entity.Properties.Add("Country", myobj.Country);
            entity.Properties.Add("IP", myobj.IP);

            if (myobj.Website!=null)
                entity.Properties.Add("Website", myobj.Website.ToString());
            else
                entity.Properties.Add("Website", "");
            entity.Properties.Add("IsApproved", myobj.IsApproved);
            entity.Properties.Add("Content", myobj.Content);
            entity.Properties.Add("DateCreated", myobj.DateCreated);
            entity.Properties.Add("PostId", myobj.Parent.Id.ToString());

            return entity;
        }

        public override object CreateObject(Entity Entity)
        {
            Comment myObj = new Comment();
            myObj.Id = new Guid(Entity.Id);
            myObj.Author = ((string)Entity.Properties.Single(tt => tt.Key == "Author").Value);
            myObj.Email = ((string)Entity.Properties.Single(tt => tt.Key == "Email").Value);
            //comment.Parent = post;
            myObj.Country = ((string)Entity.Properties.Single(tt => tt.Key == "Country").Value);
            myObj.IP = ((string)Entity.Properties.Single(tt => tt.Key == "IP").Value);


            if (!string.IsNullOrEmpty((string)Entity.Properties.Single(tt => tt.Key == "Website").Value))
            {
                Uri website;
                if (Uri.TryCreate((string)Entity.Properties.Single(tt => tt.Key == "Website").Value, UriKind.Absolute, out website))
                    myObj.Website = website;
            }
            myObj.IsApproved = ((bool)Entity.Properties.Single(tt => tt.Key == "IsApproved").Value);

            myObj.Content = ((string)Entity.Properties.Single(tt => tt.Key == "Content").Value);
            myObj.DateCreated = ((DateTime )Entity.Properties.Single(tt => tt.Key == "DateCreated").Value);
            
            return myObj;
        }


        public IEnumerable<Comment> GetPostComments(string PostId, out string errMessage)
        {
            string qry = null;
            if (PostId != null)
                qry = string.Format(" from e in entities where e.Kind == \"{0}\" && e[\"PostId\"] == \"{1}\"  select e", kind, PostId);
            return base.GetObjects(qry, out errMessage).Cast<Comment>();
        }
    }
}
